﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;
using System.Data.SqlTypes;

namespace 玩具商城管理系统.Tproduct
{
    public partial class TAdd : Form
    {
        public TAdd()
        {
            InitializeComponent();
        }
        public static string path_source = "";
        SqlDataAdapter daProduct, dalog;
        DataSet ds = new DataSet();
        void init()//初始化
        {
            DB.GetCn();
            string str = "select * from commodity";
            string sdr = "select * from log";
            daProduct = new SqlDataAdapter(str,DB.cn);
            dalog = new SqlDataAdapter(sdr,DB.cn);
            daProduct.Fill(ds, "product_info");
            dalog.Fill(ds, "log_info");
        }
        void showAll()
        {
            DataView dvProduct = new DataView(ds.Tables["product_info"]);
            dgvProduct.DataSource = dvProduct;
        }
        private void dgvProduct_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            
            

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if(txtId.Text==""||txtBook.Text==""||txtName.Text==""||txtOri.Text=="")
            {
                MessageBox.Show("必填项不能为空");
            }
            else
            {
                
                DB.GetCn();
                string str = "select * from commodity where commodity_id=' " + txtId.Text + "'";
                DataTable dt =DB.GetDataSet(str);
                if(dt.Rows.Count>0)
                {
                    MessageBox.Show("此商品已经存在，请重新输入商品编号");
                }
                else
                {
                    string filename;
                    string fileFolder;
                    string dateTime = "";
                    filename = Path.GetFileName(path_source);
                    dateTime += DateTime.Now.Year.ToString();
                    dateTime += DateTime.Now.Month.ToString();
                    dateTime += DateTime.Now.Day.ToString();
                    dateTime += DateTime.Now.Hour.ToString();
                    dateTime += DateTime.Now.Minute.ToString();
                    dateTime += DateTime.Now.Second.ToString();
                    filename = dateTime + filename;
                    fileFolder= Directory.GetCurrentDirectory()+"\\"+"Prod_Images\\"
                        +cbosort.Text+"\\";
                    fileFolder += filename;
                    DataRow drPro = ds.Tables["product_info"].NewRow();
                    drPro["commodity_id"] = int.Parse(txtId.Text);
                    drPro["commodity_name"] = txtName.Text;
                    drPro["commodity_money"]=decimal.Parse(txtMoney.Text); 
                    drPro["commodity_invent"] = int.Parse(txtInvent.Text);
                    drPro["commodity_book"] = int.Parse(txtBook.Text);
                    drPro["commodity_desc"]=txtDesc.Text;
                    drPro["sort_name"] = cbosort.Text;
                    //drPro["sort_id"] = cbosort.SelectedValue;
                    drPro["commodity_chengb"] = Decimal.Parse(txtOri.Text);
                    //drPro["commodity_id"] = Convert.ToInt32(txtId.Text);

                    if (path_source != "")
                    {
                        File.Copy(path_source, fileFolder, true);
                        drPro["commodity_picture"]="\\Prod_Images\\"+cbosort.Text
                            +"\\"+filename;
                    }
                    else
                    {
                        drPro["commodity_picture"] = "暂无图片.gif";
                    }
                    ds.Tables["product_info"].Rows.Add(drPro);

                    DataRow drlog = ds.Tables["log_info"].NewRow();
                    drlog["username"] = login.username;
                    drlog["type"] = "添加";
                    drlog["action_date"] = DateTime.Now;
                    drlog["action_table"] = "commodity表";
                    ds.Tables["log_info"].Rows.Add(drlog);

                    try
                    {
                        SqlCommandBuilder dbProduct = new SqlCommandBuilder(daProduct);
                        daProduct.Update(ds, "product_info");
                        SqlCommandBuilder dbLog = new SqlCommandBuilder(dalog);
                        dalog.Update(ds, "log_info");
                        MessageBox.Show("添加成功");
                    }
                    catch(SqlException ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                    DB.cn.Close();

                }
                
            }
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            
        }

        private void button3_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                path_source = ofd.FileName;
                pictureBox1.Image = Image.FromFile(path_source);
                pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this .Close();
        }

        private void TAdd_Load(object sender, EventArgs e)
        {
            init();
            showAll();
        }

       
    }
}
